package com.example.yls.demoa;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
    private Button btnSave;
    private  Button btnRead;
    private EditText editName;
    private EditText editAge;
    private RadioButton radioMan;
    private RadioButton radioGirl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnRead=findViewById(R.id.btn_read);
        btnSave=findViewById(R.id.btn_save);
        editName=findViewById(R.id.edit_name);
        editAge=findViewById(R.id.edit_age);
        radioMan=findViewById(R.id.radio_man);
        radioGirl=findViewById(R.id.radio_girl);


        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp = getSharedPreferences("mysp", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("isBoy",true);
                editor.putString("name","zhangsn");
                editor.putInt("age",20);
                editor.commit();
                Log.e("MainActivity","commit");

            }
        });
        btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp = getSharedPreferences("mysp", Context.MODE_PRIVATE);
                boolean isBoy = sp.getBoolean("isBoy",false);
                String name = sp.getString("name","zhangsn");
                int age = sp.getInt("age",20);
                Log.e("MainActivity","read isBoy = "+ isBoy +" name = "+ name +" age = "+ age);
            }
        });
    }
    protected  void onStop() {
        String name = editName.getText().toString();
        int age = Integer.parseInt(editAge.getText().toString());
        boolean isBoy = radioMan.isChecked();
        saveToSP(name,age,isBoy);
        super.onStop();
    }

    private void saveToSP(String name, int age, boolean isBoy) {
        SharedPreferences sp =getSharedPreferences("person",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isBoy",isBoy);
        editor.putString("name",name);
        editor.putInt("age",age);
        editor.commit();
    }
    protected void onResume() {
        super.onResume();
        SharedPreferences sp =getSharedPreferences("person",Context.MODE_PRIVATE);
        boolean isBoy = sp.getBoolean("isBoy",false);
        String name = sp.getString("name","null");
        int age = sp.getInt("age",0);
        editName.setText(name);
        editAge.setText(String.valueOf(age));
        radioMan.setChecked(isBoy);
        radioGirl.setChecked(!isBoy);
    }

}
